package core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.ui.MouseCursorData;
	import flash.utils.getTimer;
	
	import org.ais.system.Ais;
	import org.aisy.display.USprite;

	public class WinMove extends USprite
	{
		private var NAME:String;
		[Embed(source="../cursor/win_move_move.png")]
		private var _moveE:Class;
		[Embed(source="../cursor/win_move_resize.png")]
		private var _resizeE:Class;
		
		public function WinMove()
		{
		}
		
		public function connect(im:*):void
		{
			im = null;
			try {
				Ais.IMain.stage.nativeWindow;
			}
			catch (error:*) {
				clearPlugin();
				return;
			}
			
			NAME = "" + getTimer();
			
			var _v:Vector.<BitmapData> = new Vector.<BitmapData>();
			_v[0] = (new _moveE() as Bitmap).bitmapData;
			
			var _mouseCursorData:MouseCursorData = new MouseCursorData();
			_mouseCursorData.hotSpot = new Point(16, 16);
			_mouseCursorData.data = _v;
			
			Mouse.registerCursor("WIN_MOVE_MOVE" + NAME, _mouseCursorData);
			
			_v[0] = (new _resizeE() as Bitmap).bitmapData;
			
			Mouse.registerCursor("WIN_MOVE_RESIZE" + NAME, _mouseCursorData);
			
			_mouseCursorData = null;
			_v = null;
			_moveE = null;
			_resizeE = null;
			
			__addEvent();
		}
		
		public function clearPlugin():void
		{
			Ais.IMain.Swf.remove(Ais.IMain.Swf.hasSwf("swf", parent));
		}
		
		private function __addEvent():void
		{
			Ais.IMain.stage.addEventListener(MouseEvent.MOUSE_MOVE, __mouseHandler);
			Ais.IMain.stage.addEventListener(MouseEvent.MOUSE_DOWN, __mouseHandler);
		}
		
		private function __removeEvent():void
		{
			Ais.IMain.stage.removeEventListener(MouseEvent.MOUSE_MOVE, __mouseHandler);
			Ais.IMain.stage.removeEventListener(MouseEvent.MOUSE_DOWN, __mouseHandler);
		}
		
		private function __mouseHandler(e:MouseEvent):void
		{
			switch (e.type) {
				case MouseEvent.MOUSE_MOVE:
					if (e.stageX > (Ais.IMain.stage.stageWidth - 9) && e.stageY > (Ais.IMain.stage.stageHeight - 9)) {
						Mouse.cursor = "WIN_MOVE_RESIZE" + NAME;
					}
					else if (e.stageY < 18) {
						if (e.target is SimpleButton || e.target is TextField) {
							Mouse.cursor = MouseCursor.AUTO;
						}
						else {
							Mouse.cursor = "WIN_MOVE_MOVE" + NAME;
						}
					}
					else if (Mouse.cursor.indexOf("WIN_MOVE") !== -1) {
						Mouse.cursor = MouseCursor.AUTO;
					}
					break;
				case MouseEvent.MOUSE_DOWN:
					if (e.stageX > (Ais.IMain.stage.stageWidth - 9) && e.stageY > (Ais.IMain.stage.stageHeight - 9)) Ais.IMain.stage.nativeWindow.startResize();
					else if (e.stageY < 18) {
						if (e.target is SimpleButton || e.target is TextField) return;
						Ais.IMain.stage.nativeWindow.startMove();
					}
					break;
			}
			e = null;
		}
		
		override public function clear():void
		{
			Mouse.unregisterCursor("WIN_MOVE_MOVE" + NAME);
			Mouse.unregisterCursor("WIN_MOVE_RESIZE" + NAME);
			__removeEvent();
			NAME = null;
			super.clear();
		}
		
	}
}